package com.aircms.service.impl;

import com.aircms.common.ServerResponse;
import com.aircms.dao.NewsTypeMapper;
import com.aircms.pojo.NewsType;
import com.aircms.pojo.validation.BaseQueryFieldsValidation;
import com.aircms.service.INewsTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author air
 * @create 2018-09-30 下午2:31
 */

@Service(value = "iNewsTypeService")
public class NewsTypeServiceImpl implements INewsTypeService {
    @Autowired
    private NewsTypeMapper newsTypeMapper;

    public ServerResponse index(BaseQueryFieldsValidation baseQueryFields, int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<NewsType> newsTypes=newsTypeMapper.selectAll(baseQueryFields);
        PageInfo pageInfo=new PageInfo(newsTypes);
        return ServerResponse.response(pageInfo);
    }

    public ServerResponse store(NewsType newsType){
        newsTypeMapper.insertSelective(newsType);
        return ServerResponse.response();
    }

    public ServerResponse update(NewsType newsType){
        if(newsTypeMapper.updateByPrimaryKeySelective(newsType)>0){
            return ServerResponse.response();
        }else {
            return ServerResponse.response("",-1);
        }
    }

    public ServerResponse destroy(Integer id){
        if(newsTypeMapper.setStatus(id,-1)>0){
            return ServerResponse.response();
        }else {
            return ServerResponse.response("",-1);
        }
    }

    /**
     * 获取所有新闻分类
     * @param baseQueryFields
     * @return
     */
    public ServerResponse getAllNewsType(BaseQueryFieldsValidation baseQueryFields){
        List<NewsType> newsTypes=newsTypeMapper.selectAll(baseQueryFields);
        return ServerResponse.response(newsTypes);
    }



}
